function exportConfig() {	
	configManagerUpdater();
	downloadFile( JSON.stringify(emp.configManager.config) );
};


function downloadFile(content) {
	var urlObj = new Blob([content], {type:"text/plain"});	
	window.URL = window.webkitURL || window.URL;
	
	var link = document.createElement("a");
	link.download = "map-config.json";	 
	
	link.href = window.URL.createObjectURL(urlObj);
	link.dataset.downloadurl = ["text/plain", link.download, link.href].join(":");
	
	var evt = document.createEvent("mouseEvents");
	evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false,
			false, false, false, 0, null);
	
	link.dispatchEvent(evt);	
	cleanUp(link);
};


function cleanUp(link) {
	// revoke requires a delay to work properly
	setTimeout(function() {
		window.URL.revokeObjectURL(link.href);
	}, 1500);
};

  
var empConfigUrl = "mapengine/config/getMapConfiguration";
function getCurrentConfigManager() {
	emp.configManager.load({path:empConfigUrl, isSync: true});
	return emp.configManager.config;
};

function configManagerUpdater() {
	emp.$.ajax({
		type : "GET",
		url : '/map-config/admin/mapengine/config/getMapConfiguration',
		success : function(response) {
			var resp = response;
			var errors = "";
			if (resp.success == false) {
				for (var i = 0; i < resp.message.length; i++) {
					errors += resp.message[i] + "<br/>";
				}
			} else {
			}
			emp.configManager.config = resp;
			// force reloading
			emp.$("#grid-map-engine").trigger("reloadGrid");
		},
		failure : function(data) {
			alert(resp.success, errors, newId);
		},
		async:false
	});
} 


var importUrl = "http://localhost:8080/map-config/admin/mapengine/config/import";

function createButton(elem, value, cssClasses, url, method) {
	var div = emp.$('<div id="uploadDiv"></div>').appendTo(elem);
	var anch = emp.$('<a id="browse-btn" href="javascript:void(0)" class="' + cssClasses + '" >' + value + '</a>').appendTo(div);

	var height = (anch.outerHeight() <= 0) ? 25 : anch.outerHeight();
	var width  = (anch.outerWidth() <= 0) ? 159 : anch.outerWidth();
	var posTop = (height * -1);

	var form = emp.$( '<form id="uploadForm" enctype="multipart/form-data" action="' + url + '" method="' + method + '"></form').appendTo(div).css({
				'height' : '0px'
	});

	emp.$('<input name="fileInput" type="file" />').appendTo(form).css({
		'position' : 'relative',
		'overflow' : 'hidden',
		'-moz-opacity' : '0',
		'filter' : 'alpha(opacity: 0)',
		'opacity' : '0',
		'z-index' : '1002',
		'height' : height,
		'width' : width,
		'cursor' : 'pointer',
		'top' : posTop
	}).hover(function() {
		anch.addClass("manualHover");
	}, function() {
		anch.removeClass("manualHover");
	});
};

emp.$("#uploadForm").on("change", function() {
	// var f = emp.$("input#fileInput")[0].files[0];
	var fd = new FormData(emp.$("#uploadForm")[0]);
	importFile(fd);
});
    
function importFile(formData) {
    	emp.$.ajax({
			type : "POST",
			url  : importUrl,
			data : formData,
			success: function(response) {
				alert("success");
			},
			async : true,
			processData: false,
			contentType: false,
			
		});
    };

function createLink(elem, anchor, value, cssClasses, url, method) {
      var div  = emp.$('<div id="uploadDiv"></div>').appendTo(elem);
      var anch = ( anchor === undefined ) ? emp.$('<a id="browse-btn" href="javascript:void(0)" class="' + cssClasses +'" >'+ value +'</a>').appendTo(div) : anchor;

      var height = 25;
      var width  = 159;
      var posTop = ( height * -1 );              

      var form = emp.$('<form id="uploadForm" enctype="multipart/form-data" action="'+url+'" method="'+method+'"></form').appendTo(div).css({
        'height' : '0px'
      });          

      emp.$('<input id="fileInput" name="fileInput" type="file" />').appendTo(form).css({
          'position': 'relative',
          'overflow': 'hidden',
          '-moz-opacity': '0',
          'filter':  'alpha(opacity: 0)',
          'opacity': '0',
          'z-index': '1002',
          'height' : height,
          'width'  : width,
          'cursor' : 'pointer',              
          'top'    : posTop
        }).hover(function() {
          anch.addClass("manualHover");
        }, function() {
          anch.removeClass("manualHover");
        });
    };


